#!/usr/bin/env python
 # -*- coding: utf-8 -*-

from optparse import OptionParser
import os
import re

parser=OptionParser()

parser.add_option("-f", "--file", dest="filename",
                  help="write report to FILE", metavar="FILE")
parser.add_option("-y", "--youdao",
                  action="store_false", dest="youdao", default=True,
                  help="search from youdao")
parser.add_option("-b", "--baidu",
                  action="store_false", dest="baidu", default=True,
                  help="search from baidu")
parser.add_option("-e", "--English",
                  action="store_false", dest="language", default=True,
                  help="only show English information to stdout")
parser.add_option("-c", "--color",
                  action="store_false", dest="color", default=True,
                  help="color the infomation to stdout")

(options, args) = parser.parse_args()
if len(args)!=0:
	keyword=' '.join(args)
else:
   keyword=''

def get_baidu(keyword):
	#tmp=os.popen('w3m -dump http://www.baidu.com/s?wd=也许有一天你发现世界没有这么美好'+'英文')
	tmp=os.popen('w3m -dump http://www.baidu.com/s?wd='+keyword+'%20英文')
	return tmp.read()
def get_youdao(keyword):
        keyword=keyword.replace(' ','%20')
	tmp=os.popen('w3m -dump http://dict.youdao.com/w/eng/'+keyword)
        tmp=os.popen('w3m -dump http://dict.youdao.com/search?q='+keyword+'&keyfrom=fanyi.smartResult')
        return  tmp.read()

def only_english_print(para):
        ff=''.join(re.findall('[a-zA-z\n,. ]',para))
        ff=re.sub('\n+','\n',ff)
        ff=re.sub('[\[\]]','',ff)
        ff=filter(lambda x: True if x.split() else False,ff.split('\n'))
        print '\n'.join(ff)




returnstr=get_youdao(keyword)

if options.baidu!=True:
    returnstr=get_baidu(keyword)
if options.language!=True:
    only_english_print(returnstr)
else :
   print returnstr



'''
print options.youdao
print options.baidu
print (options.language)
print (options.color)
print args
'''
#only_english_print(get_youdao(keyword))
